package com.sdgs.app.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.TypeReference;
import com.sdgs.app.base.BasePage;
import com.sdgs.app.bean.UserBean;
import com.sdgs.app.dto.PageInfoDTO;
import com.sdgs.app.dto.UserDTO;
import com.sdgs.app.dto.req.UserReqDTO;
import com.sdgs.app.dto.res.EnvironmentResDTO;
import com.sdgs.app.dto.res.UserResDTO;
import com.sdgs.app.page.Page;
import com.sdgs.app.service.UserService;
import com.sdgs.app.utils.EntityDtoConvertUtil;
import com.sdgs.paas.sdk.core.PaasBaseRequest;
import com.sdgs.paas.sdk.core.PaasBaseResponse;
import com.sdgs.paas.sdk.core.web.client.PaasRestTemplate;

/**
 * Created by chenzhen on 2018/01/11.
 */
@RestController
public class DemoController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private PaasRestTemplate paasRestTemplate;
	
	@Autowired
	@Qualifier("externalRestTemplate")
	private PaasRestTemplate externalRestTemplate;  
	
    @RequestMapping("/hello")
    public String hello() {
        return "hello world！";
    }
    
    

  
	@PostMapping(value = "/getEnvironmentList")
	public PaasBaseResponse<EnvironmentResDTO> getEnvironmentList() {
		ResponseEntity<PaasBaseResponse<EnvironmentResDTO>> result = paasRestTemplate.exchange(
				"http://bizService/application/getEnvironmentList", HttpMethod.POST,
				HttpEntity.EMPTY,
				new ParameterizedTypeReference<PaasBaseResponse<EnvironmentResDTO>>() {
				});

		return result.getBody();
	}
	
	@PostMapping(value = "/getExternalEnvironmentList")
	public PaasBaseResponse<EnvironmentResDTO> getExternalEnvironmentList() {
		ResponseEntity<PaasBaseResponse<EnvironmentResDTO>> result = externalRestTemplate.exchange(
				"http://127.0.0.1:8082/application/getEnvironmentList", HttpMethod.POST,
				HttpEntity.EMPTY,
				new ParameterizedTypeReference<PaasBaseResponse<EnvironmentResDTO>>() {
				});

		return result.getBody();
	}
	
	@PostMapping(value = "/getUserList")
	public PaasBaseResponse<UserResDTO> getUserList(@RequestBody PaasBaseRequest<UserReqDTO> userReqDTO) {
//		ResponseEntity<PaasBaseResponse<EnvironmentResDTO>> result = paasRestTemplate.exchange(
//				"http://127.0.0.1:8082/application/getEnvironmentList", HttpMethod.POST,
//				HttpEntity.EMPTY,
//				new ParameterizedTypeReference<PaasBaseResponse<EnvironmentResDTO>>() {
//				});
		UserBean confFile = EntityDtoConvertUtil.convertToEntity(userReqDTO.getBody().getUserDTO(),UserBean.class);
        PageInfoDTO pageInfoDTO = userReqDTO.getBody().getPageInfo();
		
		BasePage pageRequest = new BasePage();
		pageRequest.setPage(pageInfoDTO.getPage());
		pageRequest.setCount(pageInfoDTO.getPageSize());
		Page<UserBean> page = userService.getPageFileList(pageRequest, confFile);
		
		UserResDTO userResDTO = new UserResDTO();
		PageInfoDTO resPageInfoDTO = new PageInfoDTO();
		resPageInfoDTO.setPage(page.getCurPage());
        resPageInfoDTO.setPageSize(page.getPageSize());
        resPageInfoDTO.setPageCount(page.getPageCount());
        resPageInfoDTO.setRowCount(page.getTotalCount());
        userResDTO.setPageInfo(resPageInfoDTO);
        List<UserBean> confFileList = page.getResult();
        List<UserDTO> dtoList = new ArrayList<UserDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(confFileList, new TypeReference<List<UserDTO>>() {});
		userResDTO.setUserDTOList(dtoList);
		return new PaasBaseResponse<>(userResDTO);
	}
	
	@PostMapping(value = "/saveUser")
	public PaasBaseResponse<UserResDTO> saveUser(@RequestBody PaasBaseRequest<UserReqDTO> userReqDTO) {
		UserBean confFile = EntityDtoConvertUtil.convertToEntity(userReqDTO.getBody().getUserDTO(),UserBean.class);
		Boolean flag = userService.insert(confFile);
		UserResDTO userResDTO = new UserResDTO();
		return new PaasBaseResponse<UserResDTO>(userResDTO);
	}
	
	@PostMapping(value = "/getUserDetail")
	public PaasBaseResponse<UserResDTO> getUserDetail(@RequestBody PaasBaseRequest<UserReqDTO> userReqDTO) {
		UserBean userbean = EntityDtoConvertUtil.convertToEntity(userReqDTO.getBody().getUserDTO(),UserBean.class);
		userbean = userService.getUser(String.valueOf(userbean.getUid()));
		UserResDTO userResDTO = new UserResDTO();
		UserDTO userDTO = EntityDtoConvertUtil.convertToDTO(userbean, UserDTO.class);
		userResDTO.setUserDTO(userDTO);
		return new PaasBaseResponse<UserResDTO>(userResDTO);
	}
}
